package pjkanri.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pjkanri.dao.DAOException;
import pjkanri.entity.Issure;
import pjkanri.service.IssueService;

/**
 * Servlet implementation class IssureListServlet
 */
@WebServlet("/issurelist")
public class IssureListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Issure> issueList = null;

		Integer projectId = 0;
		Object obj = request.getAttribute("projectId");
		if (obj != null) { // From add issure
			projectId = Integer.parseInt(obj.toString());
//			System.out.println("projectId1 -->> " + projectId);
		} else { // From project list
			projectId = Integer.parseInt(request.getParameter("projectId")
					.toString());

//			System.out.println("projectId2 -->> " + projectId);
		}

		try {
			issueList = new IssueService().findIssureByProjectID(projectId);
		} catch (DAOException e) {
			e.printStackTrace();
		}

		request.setAttribute("issueList", issueList);
		request.setAttribute("issureList", true);


		// セッション管理開始
		HttpSession session = request.getSession();

		// セッション・オブジェクトにAccountオブジェクトをセット
		session.setAttribute("projectId", projectId);

		String forwardPath = "_issureList.jsp";

		// 転送処理
		request.getRequestDispatcher(forwardPath).forward(request, response);
	}

}
